﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using FoobarFootball.Models.Utility;

namespace FoobarFootball.Models.MatchStates
{
    /// <summary>
    /// Deals with Matchengine logic which happens when a Match kicks off.
    /// </summary>
    public class KickOff : MatchState
    {
        private static MatchState state = new KickOff();

        /// Summary>
        /// Returns a singleton instance of this current state.
        /// </summary>
        /// <returns>MatchState</returns>
        public static MatchState Instance()
        {
            return state;
        }

        /// <summary>
        /// updates the Matchengine instance with updates that happen as a result of a kickoff.
        /// </summary>
        /// <param name="matchEngine">The current match engine</param>
        public override void Turn(MatchEngine matchEngine)
        {
            // Set score
            matchEngine.HomeTeam.Score = 0;
            matchEngine.AwayTeam.Score = 0;

            // Move ball to "centre" spot
            matchEngine.Ball.CurrentPosition = matchEngine.Pitch.Thirteen;

            // Move both sets of players to their "home" position
            matchEngine.AttackingTeam.MovePlayersToKickoffPosition(matchEngine.Pitch);
            matchEngine.DefendingTeam.MovePlayersToKickoffPosition(matchEngine.Pitch);

            // possible ball locations
            switch (RandomApi.RandomFromRange(1, 3))
            {
                case 1:
                    matchEngine.Ball.FuturePosition = matchEngine.Pitch.Seventeen;
                    break;
                case 2:
                    matchEngine.Ball.FuturePosition = matchEngine.Pitch.Eighteen;
                    break;
                case 3:
                    matchEngine.Ball.FuturePosition = matchEngine.Pitch.Nineteen;
                    break;
            }

            // possible recieving players
            matchEngine.AttackingTeam.SupportingPlayer = matchEngine.AttackingTeam.BestSupportingPlayer(matchEngine.Ball.FuturePosition);

            // possible challenging players
            matchEngine.DefendingTeam.ChallengingPlayer = matchEngine.DefendingTeam.BestChallengingPlayer(matchEngine.Ball.FuturePosition);

            // commentry
            matchEngine.Commentry.Add(string.Format("{0} get the game underway", matchEngine.AttackingTeam.Name));
            
            // Change state
            ChangeState(matchEngine, Pass.Instance());
        }
    }
}
